//
//  JDCityUnionMessageTableViewCell.swift
//  DragonWallet
//
//  Created by volantPig on 2024/5/7.
//

import UIKit

class JDCityUnionMessageTableViewCell: UITableViewCell {

    var sendPrivateMessageCallBack:((String,String,String)->Void)?
    var checkPrivateMessageCallBack:((String,String,String)->Void)?
    
    var containerView: UIView?
    var userPortraitImageV: UIImageView?
    var userName: UILabel?
    var topLine: UIView?
    var messageButton: UIButton?
    var callButton: UIButton?
    
    var descriptionLabel: UILabel?
    var imgVOne: UIButton?
    var imgVTwo: UIButton?
    var imgVThree: UIButton?
    
    var bottomLine: UIView?
    var releaseTimeLabel: UILabel?
    
    var messageModel: JDRecruitModel?
    
    var chatRecordButton: UIButton?
    var recordImgV: UIImageView?
    
    var statusLabel: JDPaddingLabel?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentView.backgroundColor = color("#F5F6F8")
        self.setupUIElements()
    }
    
    func setupUIElements() {
        
        self.containerView = UIView()
        self.contentView.addSubview(self.containerView!)
        self.containerView?.snp.makeConstraints({ make in
            make.left.equalTo(self.contentView).offset(12)
            make.right.equalTo(self.contentView).offset(-12)
            make.top.equalTo(self.contentView)
            make.bottom.equalTo(self.contentView).offset(-10)
        })
        self.containerView?.layer.cornerRadius = 8
        self.containerView?.backgroundColor = .white

        self.topLine = UIView()
        self.containerView?.addSubview(self.topLine!)
        self.topLine?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerView!).offset(12)
            make.right.equalTo(self.containerView!).offset(-12)
            make.top.equalTo(self.containerView!).offset(50)
            make.height.equalTo(1)
        })
        self.topLine?.backgroundColor = color("#F5F6F8")
        
        self.userName = UILabel()
        self.containerView?.addSubview(self.userName!)
        self.userName?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerView!).offset(41)//.offset(12)
            make.top.equalTo(self.containerView!)
            make.bottom.equalTo(self.topLine!.snp.top)
        })
        self.userName?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 14, weight: .medium), textColor: color("#333333"), numberOfLines: 1)
        self.userName?.text = "用户名"

        self.userPortraitImageV = UIImageView()
        self.containerView?.addSubview(self.userPortraitImageV!)
        self.userPortraitImageV?.snp.makeConstraints { make in
            make.centerY.equalTo(self.userName!)
            make.left.equalTo(self.containerView!).offset(12)
            make.size.equalTo(CGSizeMake(21, 21))
        }
        self.userPortraitImageV?.image = UIImage(named: "userPortraiPlaceholder_little")
        self.userPortraitImageV?.layer.cornerRadius = 10.5
        self.userPortraitImageV?.clipsToBounds = true
        
        self.callButton = UIButton(type: .custom)
        self.containerView?.addSubview(self.callButton!)
        self.callButton?.snp.makeConstraints({ make in
            make.right.equalTo(self.containerView!).offset(-12)
            make.size.equalTo(CGSizeMake(21, 21))
            make.centerY.equalTo(self.userName!)
        })
        self.callButton?.setImage(UIImage(named: "call_image")?.withRenderingMode(.alwaysOriginal), for: .normal)
        self.callButton?.addTarget(self, action: #selector(callAction), for: .touchUpInside)
        
        self.messageButton = UIButton(type: .custom)
        self.containerView?.addSubview(self.messageButton!)
        self.messageButton?.snp.makeConstraints({ make in
            make.right.equalTo(self.callButton!.snp.left).offset(-14)
//            make.centerY.equalTo(self.callButton!)
            make.top.equalTo(self.containerView!)
            make.bottom.equalTo(self.topLine!.snp.top)
            make.width.equalTo(21)
        })
        self.messageButton?.setImage(UIImage(named: "private_message"), for: .normal)
        self.messageButton?.addTarget(self, action: #selector(messageAction), for: .touchUpInside)

        self.descriptionLabel = UILabel()
        self.containerView?.addSubview(self.descriptionLabel!)
        self.descriptionLabel?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerView!).offset(12)
            make.top.equalTo(self.topLine!.snp.bottom).offset(9)
            make.right.equalTo(self.containerView!).offset(-12)
        })
        self.descriptionLabel?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 16, weight: .regular), textColor: color("#333333"), numberOfLines: 0)
        self.descriptionLabel?.text = "欢迎各位家长来巴布豆早教中心来参观，为您的孩子提供家一般的感觉"
        
        self.statusLabel = JDPaddingLabel()
        self.containerView?.addSubview(self.statusLabel!)
        self.statusLabel?.snp.makeConstraints { make in
            make.leading.equalTo(self.containerView!).offset(16)
            make.top.equalTo(self.descriptionLabel!.snp.bottom).offset(10)
            make.height.equalTo(21)
        }
        self.statusLabel?.edgeInsets = UIEdgeInsets(top: 2, left: 4, bottom: 2, right: 4)
        self.statusLabel?.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 12, weight: .regular), textColor: color("#333333"), numberOfLines: 1)
        self.statusLabel?.backgroundColor = color("#F5F6F8")
        self.statusLabel?.makeCorner(4)
        
        self.imgVOne = UIButton(type: .custom)
        self.containerView?.addSubview(self.imgVOne!)
        self.imgVOne?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerView!).offset(12)
//            make.top.equalTo(self.descriptionLabel!.snp.bottom).offset(10)
            make.top.equalTo(self.statusLabel!.snp.bottom).offset(10)
            make.size.equalTo(CGSizeMake((screenWidth-24-24-20)/3.0, (screenWidth-24-24-20)/3.0))
        })
        self.imgVOne?.isHidden = true
        
        self.imgVTwo = UIButton(type: .custom)
        self.containerView?.addSubview(self.imgVTwo!)
        self.imgVTwo?.snp.makeConstraints({ make in
            make.left.equalTo(self.imgVOne!.snp.right).offset(10)
            make.top.equalTo(self.descriptionLabel!.snp.bottom).offset(10)
            make.size.equalTo(CGSizeMake((screenWidth-24-24-20)/3.0, (screenWidth-24-24-20)/3.0))
        })
        self.imgVTwo?.isHidden = true
        
        self.imgVThree = UIButton(type: .custom)
        self.containerView?.addSubview(self.imgVThree!)
        self.imgVThree?.snp.makeConstraints({ make in
            make.left.equalTo(self.imgVTwo!.snp.right).offset(10)
            make.top.equalTo(self.descriptionLabel!.snp.bottom).offset(10)
            make.size.equalTo(CGSizeMake((screenWidth-24-24-20)/3.0, (screenWidth-24-24-20)/3.0))
        })
        self.imgVThree?.isHidden = true
        
//        self.statusLabel = JDPaddingLabel()
//        self.containerView?.addSubview(self.statusLabel!)
//        self.statusLabel?.snp.makeConstraints { make in
//            make.leading.equalTo(self.containerView!).offset(16)
//            make.top.equalTo(self.imgVThree!.snp.bottom).offset(10)
//            make.height.equalTo(21)
//        }
//        self.statusLabel?.edgeInsets = UIEdgeInsets(top: 2, left: 4, bottom: 2, right: 4)
//        self.statusLabel?.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 12, weight: .regular), textColor: color("#333333"), numberOfLines: 1)
//        self.statusLabel?.backgroundColor = color("#F5F6F8")
//        self.statusLabel?.makeCorner(4)

        self.bottomLine = UIView()
        self.containerView?.addSubview(self.bottomLine!)
        self.bottomLine?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerView!).offset(12)
            make.right.equalTo(self.containerView!).offset(-12)
            make.top.equalTo(self.imgVOne!.snp.bottom).offset(9)
//            make.top.equalTo(self.statusLabel!.snp.bottom).offset(9)
            make.height.equalTo(1)
            make.bottom.equalTo(self.containerView!).offset(-47)
        })
        self.bottomLine?.backgroundColor = color("#F5F6F8")
        
        self.releaseTimeLabel = UILabel()
        self.containerView?.addSubview(self.releaseTimeLabel!)
        self.releaseTimeLabel?.snp.makeConstraints({ make in
            make.top.equalTo(self.bottomLine!.snp.bottom)
            make.bottom.equalTo(self.containerView!)
            make.left.equalTo(self.containerView!).offset(12)
        })
        self.releaseTimeLabel?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 13, weight: .regular), textColor: color("#333333"), numberOfLines: 0)
        self.releaseTimeLabel?.text = "发布时间：3天前"
        
        self.chatRecordButton = UIButton(type: .custom)
        self.containerView?.addSubview(self.chatRecordButton!)
        self.chatRecordButton?.snp.makeConstraints({ make in
            make.centerY.equalTo(self.releaseTimeLabel!)
            make.right.equalTo(self.containerView!).offset(-12)
            make.top.equalTo(self.bottomLine!.snp.bottom)
            make.bottom.equalTo(self.containerView!)
        })
        self.chatRecordButton?.buttonConfig(normalTitle: "私聊记录", normalTitleColor: color("#333333"), titleFont: UIFont.systemFont(ofSize: 14, weight: .regular))
        self.chatRecordButton?.addTarget(self, action: #selector(privateChatRecordAction), for: .touchUpInside)
        
        self.recordImgV = UIImageView()
        self.containerView?.addSubview(self.recordImgV!)
        self.recordImgV?.snp.makeConstraints({ make in
            make.right.equalTo(self.chatRecordButton!.snp.left).offset(-4)
            make.centerY.equalTo(self.chatRecordButton!)
            make.size.equalTo(CGSize(width: 16, height: 16))
        })
        self.recordImgV?.image = UIImage(named: "msgRecord")?.withRenderingMode(.alwaysOriginal)
        
    }
    
    @objc func privateChatRecordAction() {
        self.checkPrivateMessageCallBack?(self.messageModel?.publishUserName ?? "", self.messageModel?.publishUserId ?? "",self.messageModel?.id ?? "")
    }
    
    @objc func callAction() {
        printLog("打电话\(self.messageModel?.mobile)")
        let callStr = String.init(format: "tel:%@", self.messageModel?.mobile ?? "")
        if let callUrl = URL(string: callStr) {
            UIApplication.shared.open(callUrl, options: [:]) { isSuccess in
            }
        }
    }
    
    @objc func messageAction() {
        
        self.sendPrivateMessageCallBack?(self.messageModel?.publishUserName ?? "", self.messageModel?.publishUserId ?? "",self.messageModel?.id ?? "")
        printLog("私信按钮")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    func refreshData(_ currentModel: JDRecruitModel?) {
        
        guard let confirmModel = currentModel else { return }
        self.messageModel = confirmModel
        
        self.userPortraitImageV?.downloadImage(currentModel?.headerImg ?? "", "userPortraiPlaceholder_little")
        
        self.userName?.text = confirmModel.publishUserName
        self.descriptionLabel?.text = confirmModel.content
        
        self.imgVOne?.isHidden = true
        self.imgVTwo?.isHidden = true
        self.imgVThree?.isHidden = true
        
        if (confirmModel.picUrl1?.count ?? 0) > 0 {
            self.imgVOne?.isHidden = false
            if confirmModel.picUrl1?.hasPrefix("http") ?? false  {
                self.imgVOne?.kf.setImage(with: URL(string: confirmModel.picUrl1 ?? ""), for: .normal)
            } else {
                self.imgVOne?.downloadImage(confirmModel.picUrl1 ?? "")
            }
        }
        
        if (confirmModel.picUrl2?.count ?? 0) > 0 {
            //            self.imgVTwo?.kf.setImage(with: URL(string: confirmModel.picUrl2 ?? ""), for: .normal)
            self.imgVTwo?.isHidden = false
            if confirmModel.picUrl2?.hasPrefix("http") ?? false  {
                self.imgVTwo?.kf.setImage(with: URL(string: confirmModel.picUrl2 ?? ""), for: .normal)
            } else {
                self.imgVTwo?.downloadImage(confirmModel.picUrl2 ?? "")
            }
        }
        
        if (confirmModel.picUrl3?.count ?? 0) > 0 {
            self.imgVThree?.isHidden = false
            if confirmModel.picUrl3?.hasPrefix("http") ?? false  {
                self.imgVThree?.kf.setImage(with: URL(string: confirmModel.picUrl3 ?? ""), for: .normal)
            } else {
                self.imgVThree?.downloadImage(confirmModel.picUrl3 ?? "")
            }
        }
        
        self.releaseTimeLabel?.text = String.init(format: "发布时间：%@", confirmModel.publishDate ?? "")
        
        if confirmModel.publishUserId == JDLoginModel.default.userId {
            self.messageButton?.isHidden = true
            self.callButton?.isHidden = true
//            self.recordImgV?.isHidden = true
//            self.chatRecordButton?.isHidden = true
        } else {
            self.messageButton?.isHidden = false
            self.callButton?.isHidden = false
//            self.recordImgV?.isHidden = false
//            self.chatRecordButton?.isHidden = false
        }
          
        switch  confirmModel.publishStatus ?? -1 {
        case 0:
            self.statusLabel?.text = "待审核"
            self.statusLabel?.textColor = color("#333333")
            self.statusLabel?.backgroundColor = color("#F5F6F8")
        case 1:
            self.statusLabel?.text = "上架中"
            self.statusLabel?.textColor = color("#09926C")
            self.statusLabel?.backgroundColor = color("#DEF9F2")
        case 2:
            self.statusLabel?.text = "已结束"
            self.statusLabel?.textColor = color("#FA6161")
            self.statusLabel?.backgroundColor = color("#FFF2F2")
        case 3:
            self.statusLabel?.text = "违规下架"
            self.statusLabel?.textColor = color("#9DA1B2")
            self.statusLabel?.backgroundColor = color("#F5F6F8")
        default:
            self.statusLabel?.text = "状态异常"
            self.statusLabel?.textColor = color("#333333")
            self.statusLabel?.backgroundColor = color("#F5F6F8")
        }
        
        if currentModel?.isRead == 0 {
            // 0：没有未读消息，
            self.recordImgV?.image = UIImage(named: "msgRecord")?.withRenderingMode(.alwaysOriginal)
        } else if currentModel?.isRead == 1 {
            // 1：有未读消息
            self.recordImgV?.image = UIImage(named: "msgRecord_redDot")?.withRenderingMode(.alwaysOriginal)
        }
        
        
    }
    

}
